package com.wxzl.community.common.widget

import android.content.Context
import android.graphics.Paint
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.wxzl.community.common.R
import com.wxzl.community.common.utils.AppUtils

/**
 * @Author: LiaoZhongKai
 * @Date: 2021/6/22 14:15
 * @Description:价格Textview (eg.￥200)
 */
class CommonPriceTextView(context: Context, attributeSet: AttributeSet) : AppCompatTextView(context, attributeSet) {

    private var mSymbolColor: Int = currentTextColor
    private var mSymbolSize: Int = AppUtils.sp2px(context,12f)
    private var mHasDeleteLine = false

    init {
        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CommonPriceTextView)
        mSymbolColor = typedArray.getColor(R.styleable.CommonPriceTextView_symbolColor,currentTextColor)
        mSymbolSize = typedArray.getDimensionPixelSize(R.styleable.CommonPriceTextView_symbolSize,AppUtils.sp2px(context,12f))
        mHasDeleteLine = typedArray.getBoolean(R.styleable.CommonPriceTextView_deleteLine,false)
        typedArray.recycle()
    }

    override fun setText(text: CharSequence?, type: BufferType?) {
        val format = resources.getString(R.string.common_price_format)
        val str = text?.toString()
        if (!str.isNullOrEmpty()){
            //￥49.9
            val content = format.format(str)
            val result = SpannableString(content)
            result.setSpan(AbsoluteSizeSpan(mSymbolSize),0,1, SpannableString.SPAN_INCLUSIVE_INCLUSIVE)
            result.setSpan(ForegroundColorSpan(mSymbolColor),0,1,SpannableString.SPAN_INCLUSIVE_INCLUSIVE)
            if (mHasDeleteLine){
                paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
            }
            super.setText(result, type)
        }else{
            super.setText(text, type)
        }
    }
}